#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import logging
import os
import pickle
# import demjson
import time
from com import *


url     =   "http://www.biquxs.com/book/18093/10245976.html"
endurl  =   "http://www.biquxs.com/book/18093/10515811.html"
if __name__ == '__main__':
    log = Logger('test',StreamLevel='debug',FileLevel='info').logger
    browser=init(url)
    charset=findxpath('/html/head/meta[1]')
    charset=charset.get_attribute('charset')
    charset="UTF-8" if charset==None else charset
    log.info(f"charset:{charset}")
    log.info(f"title:{browser.title}")
    file=open(str(root/f"log/{browser.title}.txt"),"w+", encoding=charset)
    while(url !=endurl):
        # presskey('end')
        tjs('contents=document.getElementById("content");')
        # tjs('contents=document.getElementsByTagName("div")[21];')
        time.sleep(0.1)
        contents=tjs("return contents.innerText;")
        contents=contents.replace(u'\xa0',' ')
        # tjs('readtitle=document.getElementsByTagName("H1")[0].innerHTML;')
        tjs('readtitle=document.title')
        
        title=tjs("return readtitle ;")
        log.info(f"{url}--->>>{title}")
        
        contents=contents.replace(title,f"\n")
        log.debug(contents)
        file.write(f"\n\n{title}\n\n")
        # file.write(f"\n")
        file.write(contents.encode(charset,'ignore').decode(charset))
        tryclick('//*[@id="book"]/div[3]/ul/li[3]/a') #下一页
        time.sleep(2)

        url=tjs("return location.href;")
    file.close()
